#!/bin/bash

server="10.29.2.38"
userName="root"
targetDir="."

function usage() {
	echo "Usage: get_ax650n.sh [-u <user_name> | -p <password> | -t <target_dir> | -h ] <file_or_dir_to_get>"
    echo "     把指定的${server}上的文件或者目录scp到本地"
    echo "     -u <user_name>   ssh的用户名称。缺省${userName}"
    echo "     -p <password>    ssh的password，缺省同<user_name>"
    echo "     -t <target_dir>  文件或者目录的复制地址，缺省为${targetDir}"
    echo "     -h               输出此帮助信息"
	exit 1
}

password="root"
while getopts "u:p:t:h" arg
do
  case "${arg}" in 
    u)
      userName="$OPTARG"
      shift 2
      ;;
    p)
      password="$OPTARG"
      shift 2
      ;;
    t)
      targetDir="$OPTARG"
      shift 2
      ;;
    *)
      usage
      ;;
  esac
done

# 检查是否指定了要上载的文件或目录
if [ $# -eq 0 ]
then
  usage
fi

src=${userName}@${server}:

for f in "$@"
do
    echo scp -r ${src}/${f} ${targetDir}
    if [ -z "${password}" ]
    then 
        scp -r ${src}/${f} ${targetDir}
    else
        sshpass -p ${password} scp -r ${src}/${f} ${targetDir}
    fi
done
